package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.CategoryMapper;
import cn.usian.dao.DishMapper;
import cn.usian.exception.DishExistsException;
import cn.usian.exception.NameExistsException;
import cn.usian.pojo.Category;
import cn.usian.pojo.Dish;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;


    @Override
    public R findPage(Integer page, Integer pageSize) {
        //创建一个查询的条件
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        //创建page对象
        Page<Category> pageInfo = new Page<>(page,pageSize);
        Page<Category> page1 = categoryMapper.selectPage(pageInfo, lambdaQueryWrapper);
        return R.success(page1);
    }

    @Override
    public R add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(1L);
        category.setUpdateUser(1L);
        categoryMapper.insert(category);
        return R.success("添加成功");
    }

//    @Override
//    public R findById(Long id) {
//        return R.success(categoryMapper.selectById(id));
//    }

    @Override
    public R updateAll(Category category) {
//        category.setUpdateTime(LocalDateTime.now());
       categoryMapper.updateById(category);
       return R.success("修改成功");
    }

    @Override
    public R deleteById(Long id) {
        //去判定当前分类是否有关联菜品，如果有关联菜品是不能直接删除
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        List<Dish> dishList = dishMapper.selectList(lambdaQueryWrapper);
        Category category = categoryMapper.selectById(id);
        //判断dishList是否为空
        if (!dishList.isEmpty()){
            //触发一个关联的自定义的菜品异常
            throw new DishExistsException(category.getName());
        }
        //直接删除
        categoryMapper.deleteById(id);
        return R.success("删除成功");
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public R findAllById(Integer type) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getType,type);
        List<Category> categoryList = categoryMapper.selectList(lambdaQueryWrapper);
        return R.success(categoryList);
    }


}
